// components/dialog/dialog.js
var app = getApp();
var address = app.globalData.address;
// console.log(address)
var util = require('../../utils/util.js');

Component({
  options: {
    multipleSlots: true // 在组件定义时的选项中启用多slot支持
  },
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    phone:null
  },
  pageLifetimes:{
    show(){
      if (app.globalData.phone) {
        console.log(app.globalData.phone)
        this.setData({
          phone: app.globalData.phone
        })
      }
    }
  },
  /**
   * 组件的方法列表
   */
  methods: {
    getPhoneNumber(e) {
      console.log(e)
      if (e.detail.errMsg == 'getPhoneNumber:fail user deny') {
        wx.showModal({
          title: '提示',
          showCancel: false,
          content: '手机号码未授权',
          success: function (res) {
            wx.navigateTo({
              url: '/pages/mall/subPackages/chat/chat',
            })
          }
        })
      } else {
        console.log(e.detail.errMsg)
        this.mobileDecode(e.detail.iv, e.detail.encryptedData);//破解手机号码
      }
    },
    // 破解手机号码
    mobileDecode(iv, encryptedData) {
      let url = address + 'Tool/mobileDecode';
      let params = {
        openId: wx.getStorageSync('openId'),
        sessionKey: wx.getStorageSync('sessionKey'),
        encryptedData: encryptedData,
        iv: iv,
      };
      util.request(url, params, function (res) {
        app.globalData.phone = res;
        wx.setStorageSync('phone', res)
        wx.navigateTo({
          url: '/pages/mall/subPackages/chat/chat',
        })
      }, function () { 
        wx.navigateTo({
          url: '/pages/mall/subPackages/chat/chat',
        })
      });

    },
    toChat() {
      let url = address + 'Interaction/chat';
      let params = { businessId: wx.getStorageSync('bId') };
      let sessionId = app.globalData.sessionId;
      util.nomeRequest(url, params, sessionId, function (res) {
        if (res == 1) {
          console.log('记录用户打开聊天成功！')
        }
      }, function () {
      });
      wx.navigateTo({
        url: '/pages/mall/subPackages/chat/chat',
      })
    },
    testSubmit(e) {
      util.testSubmit(e);
    },
  }
})
